package ru.housewife.controllers;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import ru.common.hibernate.HibernateActionExecutor;
import ru.housewife.form.DatePeriod;
import ru.housewife.form.DayMenuForm;
import ru.housewife.form.UserForm;
import ru.housewife.form.UsersTableForm;
import ru.housewife.utils.ReportUtils;

import javax.servlet.http.HttpServletRequest;
import java.beans.PropertyEditor;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * TODO: add description
 *
 * @author atroshanin
 */
@Controller
public class ReportsController extends AuthorizedController {

    private final HibernateActionExecutor hiber;

    public ReportsController(@Qualifier("houseWifesHibernate") HibernateActionExecutor hiber) {
        this.hiber = hiber;
    }

    @RequestMapping(value = "/go-shop-report-generation-form.html")
    public Object getGoShopReportGenerationForm() {
        return new ModelAndView("housewifes/reports/goShopReportGenerationPage");
    }

    @RequestMapping(value = "/get-available-users-tables-json.html")
    public Object getAvailableUsersTablesJson(DatePeriod period, HttpServletRequest request) {
        List<UsersTableForm> userTables = ReportUtils.getUserTablesForReport(getUserForm(request), period.getFrom(), period.getTo());
        return new ModelAndView("housewifes/reports/availableUsersTables-json-reply", "userTables", userTables);
    }

    @RequestMapping(value = "/generate-go-shop-report-json.html", method = RequestMethod.POST)
    public Object generateGoShopReport(DatePeriod period, @RequestParam(value = "userTablesIds[]") long [] userTablesIds, HttpServletRequest request) {
        UserForm user = getUserForm(request);
        List<UsersTableForm> usersTables = new ArrayList<UsersTableForm>(userTablesIds.length);
        for (long userTableId : userTablesIds) {
            UsersTableForm usersTable = user.getUsersTable(userTableId);
            if (usersTable == null) {
                // TODO: return ERROR here
                return null;
            }
            usersTables.add(usersTable);
        }
        if (!ReportUtils.checkUserTablesForReport(period.getFrom(), period.getTo(), usersTables)) {
                // TODO: return ERROR here
                return null;
        }

        List<DayMenuForm> dayMenus = new ArrayList<DayMenuForm>();
        for (UsersTableForm usersTable : usersTables) {
            dayMenus.addAll(ReportUtils.getDaysFromUserTableForReport(usersTable, period.getFrom(), period.getTo()));
        }
        return new ModelAndView("housewifes/reports/goShopReportResult-json-reply", "ingredientROs", ReportUtils.generateReport(dayMenus, user));
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        PropertyEditor dateEditor = new CustomDateEditor(new SimpleDateFormat("dd.MM.yyyy"), true);
        binder.registerCustomEditor(Date.class, dateEditor);
    }

}
